import { IKnowledge, IKnowledgeImg, IKnowledgeDetail } from '@/types';
import { Effect, Reducer } from 'umi';
import {
  getKnowledgeList,
  upload,
  KnowledgeDelete,
  KnowledgeState,
  getKnowledgeImgList,
  KnowledgeDetail,
} from '@/services';

export interface KnowledgeModelState {
  knowledge: IKnowledge[];
  knowledgeImgList: IKnowledgeImg[];
  knowledgeDetail: IKnowledgeDetail[];
}
export interface KnowledgeModelType {
  namespace: 'knowledge';
  state: KnowledgeModelState;
  effects: {
    getKnowledgeList: Effect;
    upload: Effect;
    KnowledgeDelete: Effect;
    KnowledgeState: Effect;
    getKnowledgeImgList: Effect;
    KnowledgeDetail: Effect;
  };
  reducers: {
    save: Reducer<KnowledgeModelState>;
  };
}
const KnowledgeModel: KnowledgeModelType = {
  namespace: 'knowledge',
  state: {
    knowledge: [],
    knowledgeImgList: [],
    knowledgeDetail: [],
  },
  effects: {
    *getKnowledgeList({ payload }, { call, put }) {
      let result = yield call(getKnowledgeList, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            knowledge: result.data,
          },
        });
      }
    },
    *upload({ payload }, { call, put }) {
      let result = yield call(upload, payload);
      if (result.statusCode == 201) {
        let result = yield call(getKnowledgeList, payload);
        if (result.data) {
          yield put({
            type: 'save',
            payload: {
              knowledge: result.data,
            },
          });
        }
      }
    },
    *KnowledgeDelete({ payload }, { call, put }) {
      let result = yield call(KnowledgeDelete, payload);
      if (result.statusCode == 200) {
        let result = yield call(getKnowledgeList, payload);
        if (result.data) {
          yield put({
            type: 'save',
            payload: {
              knowledge: result.data,
            },
          });
        }
      }
    },
    *KnowledgeState({ payload }, { call, put }) {
      let result = yield call(KnowledgeState, payload);
      console.log(result);
      if (result.statusCode == 200) {
        let result = yield call(getKnowledgeList, payload);
        if (result.data) {
          yield put({
            type: 'save',
            payload: {
              knowledge: result.data,
            },
          });
        }
      }
    },
    *getKnowledgeImgList({ payload }, { call, put }) {
      let result = yield call(getKnowledgeImgList, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            knowledgeImgList: result.data,
          },
        });
      }
    },
    *KnowledgeDetail({ payload }, { call, put }) {
      let result = yield call(KnowledgeDetail, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            knowledgeDetail: result.data,
          },
        });
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default KnowledgeModel;
